const path = require('path');
const HTMLWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const webpackMerge = require('webpack-merge');
const dev = require('./webpack.dev');
const prod = require('./webpack.prod');
const theme = require('./theme');

//是否是生产环境构建
const ENV_PROD = process.env.NODE_ENV === 'production';
const config = ENV_PROD ? prod : dev;

const commonExtractText = new ExtractTextPlugin({
  filename:'./static/style/common.css?v=[contenthash:7]',
  allChunks:true
});

const extractText = new ExtractTextPlugin({
  filename:'./static/style/[name].css?v=[contenthash:7]',
  allChunks:true
});

const plugins = [
  commonExtractText,
  extractText,
  new CopyWebpackPlugin([{
    from: __dirname + '/manifest.json',
    to:__dirname +'/dist/'
  }, {
    from: __dirname + '/static',
    to:__dirname +'/dist/static'
  }]),
  new CleanWebpackPlugin(['./dist']),
  new HTMLWebpackPlugin({
    filename:'./index.html',
    template:'./tpl/index.ejs',
    chunks:['index'],
    minify:{
      //移除空白
      collapseWhitespace:true,
      //压缩css
      minifyCSS:true,
      //压缩js
      minifyJS:true
    }
  })
]

module.exports = webpackMerge(config, {
  entry:{
    'index':'./src/index'
  },
  plugins:plugins,
  resolve:{
    extensions:['.js', '.json', '.less', '.css'],
    alias:{
      
    }
  },
  module:{
    rules:[
        {
          test:/\.js$/,
          exclude:/node_modules/,
          use:[{
            loader:'babel-loader',
            options: {
              plugins:[
                'transform-decorators-legacy',
                [
                  'import', {
                    'libraryName': 'antd',
                    'libraryDirectory': 'es', 
                    'style': true 
                  }
                ]
              ],
              presets:[
                'react',
                'stage-0'
              ]
            }
          }]
        },
        {
          test:/\.less$/,
          include:/node_modules|components|assets/,
          use:commonExtractText.extract({
            use:[{
              loader:'css-loader',
              options:{
                minimize:ENV_PROD
              }
            }, {
              loader:'less-loader',
              options:{
                javascriptEnabled:true,
                modifyVars:theme
              }
            }]
          })
        },
        {
          test:/\.less$/,
          exclude:/node_modules|components|assets/,
          use:extractText.extract({
            use:[{
              loader:'css-loader',
              options:{
                modules:true,
                minimize:ENV_PROD
              }
            }, {
              loader:'less-loader',
              options:{
                javascriptEnabled:true,
                modifyVars:theme
              }
            }]
          })
        },
        {
          test:/\.(svg|ttf|woff|eot)(\?.*)?$/,
          use: [
            {
              loader:'file-loader',
              options:{
                name:'[name].[hash:7].[ext]',
                publicPath:'./static/font/',
                outputPath:'static/font/'
              }
            }
          ]
        },
        {
          test:/\.(png|jpe?g|gif)(\?.*)?$/,
          use: [
            {
              loader:'file-loader',
              options:{
                name:'[name].[hash:7].[ext]',
                publicPath:'./static/images/',
                outputPath:'static/images/'
              }
            }
          ]
        },
        {
          test:/\.html?$/,
          use: [
            {
              loader:'html-loader'
            }
          ]
        }
      ]
  },
  output:{
    filename:'./static/script/[name].js?v=[chunkHash:7]',
    path:path.resolve(__dirname, './dist')
  }
})